package dialog;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import com.lanhai.enums.TransferType;
import com.lanhai.model.Agv;
import com.lanhai.model.MapPath;
import com.lanhai.model.Park;
import com.lanhai.model.Station;
import com.lanhai.utils.DialogUtils;
import com.lanhai.utils.HttpUtils;

import application.Main;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import net.sf.json.JSONObject;

/**
 * AGV调试(手动发送指令)
 * @author liangmin
 *
 */
public class MyTestPanel {

	//private static final Logger logger = Logger.getLogger(MyTestPanel.class);
	@FXML
	private Button start;
	@FXML
	private Button stop;
	@FXML
	private Button go_site;
	@FXML
	private ListView<Agv> listview;
	@FXML
	private Label sendData;
	@FXML
	private TextField site;
	@FXML
	private ComboBox<MapPath> agv_path;
	@FXML
	private Button left_in;
	@FXML
	private Button left_out;
	@FXML
	private ComboBox<String> line_no;
	@FXML
	private Button transfer_comp;
	@FXML
	private Button upper_transfer_out;	
	@FXML
	private Button upper_transfer_in;
	@FXML
	private Button lower_transfer_out;	
	@FXML
	private Button lower_transfer_in;	
	@FXML
	private Button out_line;
	private ObservableList<Agv> datalist = FXCollections.observableArrayList();
	private ObservableList<MapPath> pathList = FXCollections.observableArrayList();
	private ObservableList<String> lineList = FXCollections.observableArrayList();
	private Main theMain;
	
	public void init(Main main) {
		
		theMain = main;
        listview.setItems(datalist);
		for(Map.Entry<Integer, Agv> m :main.agvMap.entrySet()){;
			datalist.add(m.getValue());
		}
		
		agv_path.setItems(pathList);
		for(Map.Entry<String, MapPath> m:main.mapPathMap.entrySet()){
			pathList.add(m.getValue());
		}
		pathList = pathSort(pathList);	
		
		line_no.setItems(lineList);
		for(Map.Entry<String, Station> m:main.stationMap.entrySet()){
			lineList.add(m.getValue().getName());
		}
		/**
		 * 启动AGV
		 */
		start.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {
				Agv agv = listview.getSelectionModel().selectedItemProperty().getValue();
				if(agv == null){
					DialogUtils.show("请先选择AGV!", "提示", main.logo);
				}else{
					Map<String, Object> params = new HashMap<String, Object>();
					int startOrStop = 2;//启动
					params.put("type", "agv");
					params.put("option", 1);//启停
					params.put("agvId", agv.getId());
					params.put("action", startOrStop);
					sendData4Agv(params);
				}
			}
		});

		/**
		 * AGV停车
		 */
		stop.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {
				Agv agv = listview.getSelectionModel().selectedItemProperty().getValue();
				if(agv == null){
					DialogUtils.show("请先选择AGV!", "提示", main.logo);
				}else{
					int startOrStop = 1;//停止
					Map<String, Object> params = new HashMap<String, Object>();
					params.put("type", "agv");
					params.put("option", 1);//启停
					params.put("agvId", agv.getId());
					params.put("action", startOrStop);
					sendData4Agv(params);
				}
			}
		});	
		
		go_site.setOnAction((ActionEvent t) -> {
        	Agv agv = listview.getSelectionModel().selectedItemProperty().getValue();
        	int dot = 0;
    		MapPath mp = agv_path.getValue();
    		dot = mp==null? 0:mp.getItem();
    		System.out.println("线路号:" + dot);
        	
			if(agv == null){
				DialogUtils.show("请先选择AGV!", "提示", main.logo);
			}else if(dot == 0){
				DialogUtils.show("请先站点或线路号!", "提示", main.logo);
			}else{
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("type", "agv");
				params.put("option", 2);//下发线路
				params.put("agvId", agv.getId());
				params.put("pathId", dot);
				sendData4Agv(params);
			}
        });	
		
		left_in.setOnAction((ActionEvent t) ->{
			String mwLineStr = line_no.getValue();
			Park park = getPark(mwLineStr);
			if(park == null){
				DialogUtils.show("请先选择机台!", "提示", main.logo);		
			}else{
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("path", "submit");//请求路径
				params.put("type", "park");
				params.put("parkId", park.getId());
				params.put("action", 1);//动作，1：到位
				main.httpRequest(params);		
			}
		});

		left_out.setOnAction((ActionEvent t) ->{
			String mwLineStr = line_no.getValue();
			Park park = getPark(mwLineStr);
			if(park == null){
				DialogUtils.show("请先选择机台!", "提示", main.logo);		
			}else{
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("path", "submit");//请求路径
				params.put("type", "park");
				params.put("parkId", park.getId());
				params.put("action", 2);//动作，2：离开
				main.httpRequest(params);			
			}	
		});		
		
		transfer_comp.setOnAction((ActionEvent t) ->{
			Agv agv = listview.getSelectionModel().selectedItemProperty().getValue();
			if(agv == null){
				DialogUtils.show("请先选择AGV!", "提示", main.logo);
			}else{
				int type = 1;//1,传输完成
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("type", "agv");
				params.put("option", 3);//下发线路
				params.put("agvId", agv.getId());
				params.put("action", type);
				sendData4Agv(params);			
			}
		});

		upper_transfer_out.setOnAction((ActionEvent t) ->{
			Agv agv = listview.getSelectionModel().selectedItemProperty().getValue();
			if(agv == null){
				DialogUtils.show("请先选择AGV!", "提示", main.logo);
			}else{
				int type = TransferType.SCC.getCode();//上层传出
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("type", "agv");
				params.put("option", 3);//下发线路
				params.put("agvId", agv.getId());
				params.put("action", type);
				sendData4Agv(params);			
			}
		});		

		upper_transfer_in.setOnAction((ActionEvent t) ->{
			Agv agv = listview.getSelectionModel().selectedItemProperty().getValue();
			if(agv == null){
				DialogUtils.show("请先选择AGV!", "提示", main.logo);
			}else{
				int type = TransferType.SCR.getCode();//上层传入
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("type", "agv");
				params.put("option", 3);//下发线路
				params.put("agvId", agv.getId());
				params.put("action", type);
				sendData4Agv(params);			
			}
		});	

		lower_transfer_out.setOnAction((ActionEvent t) ->{
			Agv agv = listview.getSelectionModel().selectedItemProperty().getValue();
			if(agv == null){
				DialogUtils.show("请先选择AGV!", "提示", main.logo);
			}else{
				int type = TransferType.XCC.getCode();//下层传出
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("type", "agv");
				params.put("option", 3);//下发线路
				params.put("agvId", agv.getId());
				params.put("action", type);
				sendData4Agv(params);			
			}
		});	
		lower_transfer_in.setOnAction((ActionEvent t) ->{
			Agv agv = listview.getSelectionModel().selectedItemProperty().getValue();
			if(agv == null){
				DialogUtils.show("请先选择AGV!", "提示", main.logo);
			}else{
				int type = TransferType.XCR.getCode();//下层传入
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("type", "agv");
				params.put("option", 3);//下发线路
				params.put("agvId", agv.getId());
				params.put("action", type);
				sendData4Agv(params);			
			}
		});			
		
	}
	
	public ObservableList<MapPath> pathSort(ObservableList<MapPath> pathList){
        Collections.sort(pathList, new Comparator<Object>() {
            public int compare(Object a, Object b) {
                int one = ((MapPath) a).getId();
                int two = ((MapPath) b).getId();
                return one - two;
            }
        });	
        return pathList;
	}	
	
	private void sendData4Agv(Map<String, Object> params){
		String url = "http://" + theMain.serverUrl + "/submit";
		JSONObject jo = HttpUtils.httpPost2Json(url, params);
		if(!jo.isEmpty()){
			boolean flag = jo.getBoolean("success");
			if(flag){
				sendData.setText(jo.getString("sendData"));
				//DialogUtils.show("成功发送数据!", "提示", theMain.logo);
			}else{
				DialogUtils.show("数据错误!", "提示", theMain.logo);
			}
		}else{
			DialogUtils.show("远程服务器访问失败!", "提示", theMain.logo);
		}
	}
	
	public Park getPark(String name) {
		Park park = null;
		for(Map.Entry<Integer, Park> m:theMain.parkMap.entrySet()){
			Park p = m.getValue();
			if(p.getName().equals(name)){
				park = p;
			}
		}
		return park;
	}
	
}
